package com.deer.wms.ware.task.model.pushErp.enter;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.deer.wms.base.system.model.Item.ItemInfo;
import com.deer.wms.base.system.model.OrderDetailBO;
import com.deer.wms.base.system.model.OrderToErpBO;
import com.deer.wms.base.system.service.ItemInfoService;
import com.deer.wms.project.root.util.DateUtils;
import com.deer.wms.system.manage.service.CodeRuleService;
import com.deer.wms.ware.task.model.SO.SoMaster;
import com.deer.wms.ware.task.model.enums.SoTypeResolveEnum;
import com.deer.wms.ware.task.model.out.OutDetail;
import com.deer.wms.ware.task.model.pushErp.PushErpBean;
import com.deer.wms.ware.task.model.pushErp.beasAssist.BaseAssist;
import com.deer.wms.ware.task.service.PushErpService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author: xu
 * @Date: 2022/06/17/10:02
 * @Description: 销售出库  21
 */
public class MarketEnterBean implements PushErpBean {

    @Autowired
    private CodeRuleService codeRuleService;
    @Autowired
    private PushErpService pushErpService;
    @Autowired
    private ItemInfoService itemInfoService;


    @Override
    public Integer gainFileType() {
        return SoTypeResolveEnum.MARKET.getStatenum();
    }

    @Override
    public Map<String, String> getBeanAndPush(SoMaster soMaster, List<OutDetail> outDetails, BaseAssist enterAssist) {
        OrderToErpBO orderToErpBO = new OrderToErpBO();
        orderToErpBO.setBillTypeId(soMaster.getSoType());
        orderToErpBO.setSourceBillTypeId(soMaster.getBillType()); //
        orderToErpBO.setBillNo(codeRuleService.generateCodeByRedis("OUT"));
        orderToErpBO.setBillDate(DateUtils.getNowDateString());
//        orderToErpBO.setEmpId(ObjectUtil.isNotEmpty(enterAssist.getEmpId()) ? enterAssist.getEmpId() : 41); //业务员id
        orderToErpBO.setCategoryId(ObjectUtil.isNotEmpty(enterAssist.getCategoryId()) ? enterAssist.getCategoryId() : 41146); //默认其他调拨
        List<OrderDetailBO> details = new ArrayList<>();
        outDetails.forEach(item -> {
            OrderDetailBO orderDetailBO = new OrderDetailBO();
            orderDetailBO.setSourceBillId(item.getErpBillId());//源单据id
            orderDetailBO.setSourceBillDetailRowIndex(item.getBomSplit());//行号
//            orderDetailBO.setAuxPropId(item.getPackDetailId());//辅助类型id
            orderDetailBO.setQty(item.getDeliveryQuantity());//数量
            ItemInfo itemInfo = itemInfoService.getOne(new QueryWrapper<ItemInfo>().eq("item_code", item.getItemCode()));
            orderDetailBO.setItemId(itemInfo.getErpItemId());
            orderDetailBO.setBatchNo(item.getBatchName()); //计划跟踪号
            orderDetailBO.setStockId(item.getErpAreaId()); //调出仓库
            orderDetailBO.setStockId2(enterAssist.getStockId2()); //调入仓库
//            orderDetailBO.setBoxCode(item.getBoxCode());
            details.add(orderDetailBO);
        });
        orderToErpBO.setDetails(details);
        return  pushErpService.pushIMBIll(orderToErpBO);

    }
}
